package com.atguigu.FileIO;

import java.io.*;

/**
 * Java API提供了两个转换流:字节流和字符流之间的转换
 *  1.InputStreamReader:将InputStream转换为Reader
 *  2.OutputStreamWriter:将Writer转换为OutputStream
 * 为什么要转换：字节流中的数据都是字符时，字符流操作更高效。转换流处理文件乱码问题
 * 字节流->字符流
 *  构造器：
 *      public InputStreamReader(InputStream in)
 *      public InputStreamReader(InputStream in, String charsetName)
 * 字符流转换为字节流：
 *  构造器
 *      public OutStreamWriter(OutputStream out)
 *      public OutStreamWriter(OutputStream out, String charsetName)
 * @author xiaoqi
 * @data 2021/04/25 21:05
 */
public class TransformStream {
    public static void main(String[] args) throws IOException {
        //设置读取对象
        FileInputStream fis = new FileInputStream(new File("D:\\Program Files (x86)\\IdeaProjects\\JavaSenior\\src\\com\\atguigu\\FileIO\\test1.txt"));

//        //转换为字符流
//        InputStreamReader isr = new InputStreamReader(fis);
//        //读取长度
        int length = 0;
        //字节流
        byte[] by = new byte[5];
//        //保存
//        char[] ch = new char[5];
//        while((length = isr.read(ch)) != -1){
//            String str = new String(ch, 0 , length);
//            System.out.println(str);
//        }
//        isr.close();
        while ((length = fis.read(by)) != -1){
            String str = new String(by, 0, length);
            System.out.println(str);
        }

    }
}
